package com.offcn.dao;

import com.offcn.entity.Course;
import com.offcn.utils.PageTool;

import java.util.List;

public class CourseDao extends BaseDao<Course> {
    BaseDao baseDao = new BaseDao();
    public int findCourseCount(String search) {
        String sql ="select count(*) from course";
        if (search!=null){
            sql+=" where courseName like '%"+search+"%'";
        }
        long l = (long) baseDao.singLeData(sql);
        return (int) l;
    }

    public List<Course> findAllCourse(PageTool pageTool, String search) {
        String sql ="select * from course";
        if (search!=null){
            sql+=" where courseName like '%"+search+"%'";
        }
        sql+=" limit ?, ?";
        List list = baseDao.moreline(sql, Course.class,pageTool.getStartIndex(),pageTool.getPageSize());
        return list;
    }

    public int addCourse(Course course) {
        return baseDao.dml("insert into course values(null , ?, ? , ? , ? , ? , ? , ? ,now())",
                course.getCourseName(),course.getDescs(),course.getCourseType(),course.getCourseImage(),
                course.getCourseVideo(),course.getCoursePrice(),course.getStatus()
                );
    }

    public int deleteAllCourse(String deleteCourseUid) {
        return baseDao.dml( "delete from course where cid in ("+ deleteCourseUid+ ")");
    }

    public Course selectFileName(String s) {
        return singleline("select * from course where cid = ?",Course.class,s);
    }

    public int updateCourse(Course course) {
        return dml("update course set courseName = ? , descs = ? , courseType = ? , courseImage = ? , courseVideo = ? , coursePrice = ? ,status = ?  where cid = ?",
                course.getCourseName(),course.getDescs(),course.getCourseType(),course.getCourseImage(),course.getCourseVideo(),course.getCoursePrice(),course.getStatus(),course.getCid() );
    }



    public Course findCourseByCid(String s) {
        return singleline("select * from course where cid = ?",Course.class,s);
    }

    public List<Course> findAllCourses() {
        return moreline("select * from course where status = 1 ",Course.class);
    }
}
